package org.wanho.rent.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import org.wanho.rent.entity.Company;
import org.wanho.rent.entity.LeadingPeriod;
import org.wanho.rent.entity.ProductType;
import org.wanho.rent.entity.SysUser;
import org.wanho.rent.factory.ObjectFactory;

import org.wanho.rent.util.AjaxResult;
import org.wanho.rent.util.PageInfo;
import org.wanho.rent.util.ResponseUtil;
import org.wanho.rent.vo.ReportDetailVO;
import org.wanho.rent.vo.ReportVO;

/**
 * 公司控制器
 * @author student
 *
 */
@WebServlet(urlPatterns = "*.company")
public class CompanyController extends HttpServlet {

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		System.out.println(request.getServletPath() + "###");
		String servletPath = request.getServletPath();
		if (servletPath.equals("/addCompany.company")) {
			addCompany(request, response);
			System.out.println("-========+/addCompany");
		} else if (servletPath.equals("/updateCompany.company")) {
			updateCompany(request, response);
		} else if (servletPath.equals("/deleteCompany.company")) {
			deleteCompany(request, response);
		} else if (servletPath.equals("/selectAllCompany.company")) {
			selectAllCompany(request, response);
		} else if (servletPath.equals("/selectCompanyById.company")) {
			selectCompanyById(request, response);
		} else if (servletPath.equals("/selectCompanyReport.company")) {
			selectCompanyReport(request, response);
		} else if (servletPath.equals("/selectCompanyReportDetail.company")) {
			selectCompanyReportDetail(request, response);

		}

	}

	/**
	 * 报表明细(详情)
	 * <一句话功能简述>
	 * <功能详细描述>
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 * @see [类、类#方法、类#成员]
	 */
	private void selectCompanyReportDetail(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		String pageNo = request.getParameter("pageNo");
		String companyId = request.getParameter("companyId");
		System.out.println(pageNo + "=pageNo , companyId=" + companyId);

		int page = Integer.parseInt(pageNo);

		List list = new ArrayList();

		list.add(new ReportDetailVO(1, "产品A", 100, 80, 20.0));
		list.add(new ReportDetailVO(2, "产品B", 100, 80, 20.0));
		list.add(new ReportDetailVO(3, "产品B", 100, 80, 20.0));
		// 返回数据到浏览器

		int pagesize = 2;
		if (page == 2) {
			ResponseUtil.doResponse("成功！",
					new PageInfo<ReportDetailVO>(3, page, 2, list.subList((page - 1) * pagesize, list.size())), true,
					response);

		} else {
			ResponseUtil.doResponse("成功！",
					new PageInfo<ReportDetailVO>(3, page, 2, list.subList((page - 1) * pagesize, pagesize * page)),
					true, response);
		}

	}

	/**
	 * 查询公司报表
	 * <一句话功能简述>
	 * <功能详细描述>
	 * @param request
	 * @param response
	 * @see [类、类#方法、类#成员]
	 */
	private void selectCompanyReport(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String pageNo = request.getParameter("pageNo");
		String year = request.getParameter("year");
		System.out.println(pageNo + "=pageNo , year=" + year);
		int page = Integer.parseInt(pageNo);

		List list = new ArrayList();

		list.add(new ReportVO(1, "江苏银行", 4, 1200, 800, 60.0));
		list.add(new ReportVO(2, "江苏银行", 4, 1200, 800, 60.0));
		list.add(new ReportVO(3, "江苏银行", 4, 1200, 800, 60.0));
		// 返回数据到浏览器

		int pagesize = 2;
		if (page == 2) {
			ResponseUtil.doResponse("成功！",
					new PageInfo<ReportVO>(3, page, 2, list.subList((page - 1) * pagesize, list.size())), true,
					response);

		} else {
			ResponseUtil.doResponse("成功！",
					new PageInfo<ReportVO>(3, page, 2, list.subList((page - 1) * pagesize, pagesize * page)), true,
					response);
		}

	}

	/**
	 * 根据id查询一个公司，用来做修改前的查询
	 * @param request
	 * @param response
	 */
	private void selectCompanyById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String companyId = request.getParameter("companyId");
		System.out.println(companyId + "公司id");

		// 返回数据到浏览器
		ResponseUtil.doResponse("成功！", new Company(1, "中国银行", "中国银行是国有银行，中国银行是国有银行中...", 0.03, ""), true, response);
	}

	/**
	 * 查询所有公司
	 * @param request
	 * @param response
	 */
	private void selectAllCompany(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String all = request.getParameter("all");// "all"
		// all不是null的时候就是查询所有，否则必须分页查询
		if (all != null) {

			List list = new ArrayList();

			list.add(new Company(1, "中国银行", "中国银行是国有银行，中国银行是国有银行中...", 0.03, ""));
			list.add(new Company(2, "公司B", "中等公司", 0.02, ""));
			list.add(new Company(3, "公司C", "初创小公司", 0.03, ""));
			// 返回数据到浏览器
			ResponseUtil.doResponse("成功！", list, true, response);
			return;

		} else {

			String pageNo = request.getParameter("pageNo");
			System.out.println(pageNo + "#");
			int page = Integer.parseInt(pageNo);

			List list = new ArrayList();

			list.add(new Company(1, "中国银行", "中国银行是国有银行，中国银行是国有银行中...", 0.03, ""));
			list.add(new Company(2, "公司B", "中等公司", 0.02, ""));
			list.add(new Company(3, "公司C", "初创小公司", 0.03, ""));
			// 返回数据到浏览器

			int pagesize = 2;
			if (page == 2) {
				ResponseUtil.doResponse("成功！",
						new PageInfo<Company>(3, page, 2, list.subList((page - 1) * pagesize, list.size())), true,
						response);

			} else {
				ResponseUtil.doResponse("成功！",
						new PageInfo<Company>(3, page, 2, list.subList((page - 1) * pagesize, pagesize * page)), true,
						response);
			}

		}

	}

	/**
	 * 删除公司
	 * @param request
	 * @param response
	 */
	private void deleteCompany(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String companyId = request.getParameter("companyId");
		System.out.println("删除的id=" + companyId);

		// 返回数据到浏览器
		ResponseUtil.doResponse("删除成功！", null, true, response);
	}

	/**
	 * 修改公司
	 * @param request
	 * @param response
	 */
	private void updateCompany(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String companyId = request.getParameter("companyId");
		String companyName = request.getParameter("companyName");
		String productRate = request.getParameter("productRate");

		String content = request.getParameter("content");

		System.out.println(companyId + " " + companyName + " " + productRate + " " + content);
		// "companyId":$("#companyId").val(),
		// "companyName":$("#companyName").val(),
		// "productRate":$("#productRate").val(),
		// "content":$("#content").text()

		// 返回数据到浏览器
		ResponseUtil.doResponse("成功！", null, true, response);
	}

	/**
	 * 添加公司
	 * @param request
	 * @param response
	 */
	private void addCompany(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String companyName = request.getParameter("companyName");
		String productRate = request.getParameter("productRate");

		String content = request.getParameter("content");

		System.out.println(companyName + " " + productRate + " " + content);

		Company c = new Company(null, companyName, content, Double.parseDouble(productRate), null);

		try {
			// companyproxy.addCompany(c);
			// 返回数据到浏览器

			ResponseUtil.doResponse("添加公司成功！", null, true, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			// 返回数据到浏览器
			ResponseUtil.doResponse("添加公司失败！", null, false, response);
		}

	}

}
